﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZkSurvey.BLL.SurveyMgr;
using ZkSurvey.Common.SurveyMgr;
using ZkSurvey.Web.Helper;
using ZkSurvey.Web.Helper.Extensions;
namespace ZkSurvey.Web.VP.SurveyMgr
{
    public partial class Manage : System.Web.UI.UserControl
    {
        SurveyBl surveyBl = new SurveyBl();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindSurvey();
            }
            MessageHelper.Prepare(this.Page);
        }

        private void BindSurvey()
        {
            List<Survey> surveies = surveyBl.GetSurveyList();
            GvList.DataSource = surveies;
            GvList.DataBind();

            ltrSvCountInfo.Text = string.Format("问卷总计：{0} [进行中:{1}; 已结束:{2}; 未开始:{3}]", surveies.Count, surveies.RunningCount(), surveies.CompletedCount(), surveies.NotStartdCount());
        }

        protected void BtnSaveNewSurvey_Click(object sender, EventArgs e)
        {
            Survey survey = new Survey();
            survey.Title = tbxTitle.Text.Trim();
            survey.Description = tbxDescription.Text.Trim();
            survey.StartTime = tbxStartTime.Date.Value;
            survey.EndTime = tbxEndTime.Date.Value;
            survey.Creator = UserHelper.UserName;


            try
            {
                surveyBl.CreateSurvey(survey);
                BindSurvey();
                MessageHelper.RegShowJSAjax(this.BtnSaveNewSurvey, "创建问卷成功!", this.Page);
            }
            catch (ApplicationException ex)
            {
                MessageHelper.RegShowJSAjax(this.BtnSaveNewSurvey, ex.Message, this.Page);
            }
        }

        protected void LBtnNewSurvey_Click(object sender, EventArgs e)
        {
            //tbxValidationCode.Text = "aaddss";//TODO:生成随机字符串,写在bll层

            MpeNewSurvey.Show();
        }

        protected void GvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GvList.ShowRowNumber(e);

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Survey survey = (Survey)e.Row.DataItem;

                HyperLink hlEdit = (HyperLink)e.Row.FindControl("HlEdit");

                ((HyperLink)e.Row.FindControl("HlView")).NavigateUrl = UrlHelper.GenerateUrl("SvDetail", "id", survey.Id.ToString());
                ((HyperLink)e.Row.FindControl("HlExtView")).NavigateUrl = string.Format("~/External/Submit.aspx?code={0}&vcode={1}", survey.Code, survey.ValidationCode);


                if (survey.Status == SurveyStatus.NotStarted)
                {
                    hlEdit.NavigateUrl = UrlHelper.GenerateUrl("SvEdit", "id", survey.Id.ToString());
                }
                else if (survey.Status == SurveyStatus.Running)
                {
                    hlEdit.NavigateUrl = UrlHelper.GenerateUrl("SvEdit", "id", survey.Id.ToString());
                }
                else if (survey.Status == SurveyStatus.Completed)
                {
                    hlEdit.NavigateUrl = "#";
                    hlEdit.Attributes["OnClick"] = "alert('问卷调查已经结束，不允许编辑');return false;";
                }

            }
        }

        protected void GvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GvList.PageIndex = e.NewPageIndex;
            BindSurvey();
        }

        protected void GvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //删除问卷
            if (e.CommandName == "DeleteSv")
            {
                int svId = Convert.ToInt32(e.CommandArgument);

                try
                {
                    surveyBl.DeleteSurvey(svId);
                    BindSurvey();
                    MessageHelper.RegShowJSAjax(this.GvList, "删除成功", this.Page);
                }
                catch (ApplicationException ex)
                {
                    MessageHelper.RegShowJSAjax(this.BtnSaveNewSurvey, ex.Message, this.Page);
                }
                //catch (Exception ex)
                //{
                //    ErrorHelper.Handle(ex); 
                //}
            }
        }
    }
}